package com.mxx.common.exception;

import com.fasterxml.jackson.core.JsonLocation;
import com.fasterxml.jackson.core.JsonProcessingException;

import java.io.IOException;

/**
 * @Auther: Mr. Zeng
 * @Date: 2020/8/12 16:46
 * @Description: 暂无描述
 */
public class JsonProcessException extends RuntimeException {
    static final long serialVersionUID = 123L;
    protected JsonLocation _location;

    protected JsonProcessException(String msg, JsonLocation loc, Throwable rootCause) {
        super(msg);
        if (rootCause != null) {
            this.initCause(rootCause);
        }

        this._location = loc;
    }

    public JsonProcessException(String msg) {
        super(msg);
    }

    public JsonProcessException(JsonProcessingException ex) {
        super("data parse exception", ex);
        this._location = ex.getLocation();
    }

    public JsonProcessException(String failureMessage, JsonProcessException ex) {
        super(failureMessage, ex);
        this._location = ex.getLocation();
    }

    public JsonProcessException(IOException ex) {
        super("data read io exception", ex);
    }

    public JsonLocation getLocation() {
        return this._location;
    }

    public void clearLocation() {
        this._location = null;
    }

    public String getOriginalMessage() {
        return super.getMessage();
    }


    protected String getMessageSuffix() {
        return null;
    }

    public String getMessage() {
        String msg = super.getMessage();
        if (msg == null) {
            msg = "N/A";
        }

        JsonLocation loc = this.getLocation();
        String suffix = this.getMessageSuffix();
        if (loc != null || suffix != null) {
            StringBuilder sb = new StringBuilder(100);
            sb.append(msg);
            if (suffix != null) {
                sb.append(suffix);
            }

            if (loc != null) {
                sb.append('\n');
                sb.append(" at ");
                sb.append(loc.toString());
            }

            msg = sb.toString();
        }

        return msg;
    }

    public String toString() {
        return this.getClass().getName() + ": " + this.getMessage();
    }
}
